View Javadoc
1   package org.apache.maven.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  
26  /**
27   * Write the trace out for a POJO test.
28   *
29   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30   */
31  public class PojoStackTraceWriter
32      implements StackTraceWriter
33  {
34      private final Throwable t;
35  
36      private final String testClass;
37  
38      private final String testMethod;
39  
40      public PojoStackTraceWriter( String testClass, String testMethod, Throwable t )
41      {
42          this.testClass = testClass;
43          this.testMethod = testMethod;
44          this.t = t;
45      }
46  
47      public String writeTraceToString()
48      {
49          if ( t != null )
50          {
51              StringWriter w = new StringWriter();
52              PrintWriter stackTrace = new PrintWriter( w );
53              try
54              {
55                  t.printStackTrace( stackTrace );
56              }
57              finally
58              {
59                  stackTrace.close();
60              }
61              w.flush();
62              StringBuffer builder = w.getBuffer();
63              if ( isMultiLineExceptionMessage( t ) )
64              {
65                  // SUREFIRE-986
66                  String exc = t.getClass().getName() + ": ";
67                  if ( builder.toString().startsWith( exc ) )
68                  {
69                      builder.insert( exc.length(), '\n' );
70                  }
71              }
72              return builder.toString();
73          }
74          return "";
75      }
76  
77      public String smartTrimmedStackTrace()
78      {
79          return t == null ? "" : new SmartStackTraceParser( testClass, t, testMethod ).getString();
80      }
81  
82      public String writeTrimmedTraceToString()
83      {
84          return t == null ? "" : SmartStackTraceParser.stackTraceWithFocusOnClassAsString( t, testClass );
85      }
86  
87      public SafeThrowable getThrowable()
88      {
89          return t == null ? null : new SafeThrowable( t );
90      }
91  
92      private static boolean isMultiLineExceptionMessage( Throwable t )
93      {
94          String msg = t.getLocalizedMessage();
95          if ( msg != null )
96          {
97              int countNewLines = 0;
98              for ( int i = 0, length = msg.length(); i < length; i++ )
99              {
100                 if ( msg.charAt( i ) == '\n' )
101                 {
102                     if ( ++countNewLines == 2 )
103                     {
104                         break;
105                     }
106                 }
107             }
108             return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith( "\n" );
109         }
110         return false;
111     }
112 }